<?php
namespace Home\Controller;
use Think\Controller;
class PayController extends CommonController{

	// public $product ;
	// public $inventory ;
	// public $color ;
	// public $size ;
	public $goods_cart;
	public $address;
	

	public function _initialize(){
		// $this->product = M('product as a');

		// $this->inventory = M('pro_inventory');

		// $this->color = M('color');

		$this->address = M('address');

		$this->goods_cart = M('goods_cart as a');
	}

	public function confirm_order(){
		if(session('user_id')==''){
			$this->redirect('Login/login');
		}
		if(IS_AJAX){
			$info = ['code' => 0 , 'status' => false , 'info' => ''];
			

			$orders = D('orders');

			
			if(!$orders->create()){
				$info['info'] = $orders->getError();
				$this->ajaxReturn($info);
			}else{
				if(!empty(session('order_id'))){

				}else{
					$result = $orders->add();
					if($result){
						$info['code'] = 1;
						$info['status'] =true;
						$info['order_id'] = $result;

						//更改购物车状态
						$cart_ids = explode(',',session('cart_ids'));
						$goods_cart = M('goods_cart');
						foreach($cart_ids as $val){
							$goods_cart->where(array('cart_id'=>$val))->save(array('statu'=>1));
						}
						$this->ajaxReturn($info);
					}else{
						$info['info'] = '订单生成失败';
						$this->ajaxReturn($info);
					}
				}
			}
			// var_dump(session('cart_ids'));
		}else{
			
				$cart_ids = explode(',',session('cart_ids'));
				$order_info = array();
				$field = 'a.pro_id,a.size_id,a.number,a.cart_id,b.pro_name,c.color_name,d.size_scope,f.catename,g.price';
				
				foreach($cart_ids as $val){
					$order_info[] = $this->goods_cart->field($field)->where(array('cart_id' => $val))->join('rl_product as b on b.pro_id=a.pro_id')->join('rl_color as c on c.color_id=a.color_id')->join('rl_size as d on d.size_id=a.size_id')->join('rl_pro_cate as e on e.pro_id=a.pro_id')->join('rl_category as f on f.cate_id=e.cate_id')->join('rl_pro_inventory as g on g.size_id=a.size_id and g.color_id=a.color_id and g.pro_id=a.pro_id')->find();
				}

				
				// dump($order_info);die;
			


			$defau = M('default');
			$defaul = $defau->where(array('table'=>'address'))->find();
			// var_dump($defaul);die;
			$addre = $this->address->where(array('user_id'=>session('user_id')))->select();
			
			$this->assign(array(
				'order_info'	=>	$order_info,
				'defaul'		=>	$defaul,
				'address'		=>	$addre,
				'cart_ids'		=>	session('cart_ids'),
				// 'las'			=>	$las,
			));
			// session('cart_ids',NULL);
			// session('way',NULL);
			$this -> display();
			

		}

	}

	

	public function defau(){
		$defau = M('default');
		$result = $defau->where(array('table'=>'address'))->save(array('default_id'=>I('post.address_id')));
		if(!$result){
			$info['info'] = '设置失败';
			$this->ajaxReturn($info);
		}else{
			$info['code'] = 1;
			$info['status'] = true;
			$info['info'] = '设置成功';
			$this->ajaxReturn($info);
		}	
	}

	public function pay(){
		if(session('user_id')==''){
			$this->redirect('Login/login');
		}
		$orders = D('orders');
		$order_id = I('get.order_id');
		
		$order_info = $orders->where(array('order_id'=>$order_id))->find();
		$this->assign(array(
			'order_info'		=>	$order_info,
		));
		$this->display();

	}

	public function pay_success(){
		if(session('user_id')==''){
			$this->redirect('Login/login');
		}
		$order_id = I('get.order_id');
		$orders = M('orders as a');
		$order_info = $orders->join('rl_address as b on b.address_id = a.address_id')->where(array('order_id'=>$order_id))->find();

		
		$goods_cart = M('goods_cart');
		$inventory = M('pro_inventory');
		$cart_ids = explode(',',$order_info['cart_ids']);
		$orders = M('orders');
		$orders->where(array('order_id'=>$order_id))->save(array('statu'=>1));
		foreach($cart_ids as $val){
			$goods_cart->where(array('cart_id'=>$val))->save(array('statu'=>2));
			$goods_info = $goods_cart->where(array('cart_id'=>$val))->find();
			
			$inven = $inventory->where(array('color_id'=>$goods_info['color_id'],'size_id'=>$goods_info['size_id'],'pro_id'=>$goods_info['pro_id']))->setDec('inventory',$goods_info['number']);

		}

		session('cart_ids',NUll);
		$this->assign(array(
			'order_info'		=>	$order_info,
		));
		$this->display();
	}


}


	



?>